<?php
/*
Extension Name: MarkAllRead
Extension Url: mail@hutstein.de
Description: An extension, that enables users to mark discussions as read
Version: 1.0.1
Author: Andreas Hutstein
Author Url: http://www.hutstein.de/

You should cut & paste these language definitions into your conf/your_language.php file
(replace "your_language" with your chosen language, of course):
*/
#Here you can config, which discussions this extension marks as read. 
#For big forums it's better to set this to a small time and for small forums you can use bigger limits:
#You can use a number and an character like : DAY for days or w for weeks. For example: 6d or 1w or 3w
$Configuration["MARKALLREAD_LIMIT"] = '2d';

$Context->Dictionary['MarkAllRead'] = 'Mark All Read';
$Context->Dictionary['MarkCategoryRead'] = 'Mark Category Read';



if( $Context->Session->UserID > 0) 	{

	function MarkAllRead(&$Context) {
		$UserID = $Context->Session->UserID;
		$Number = substr($Context->Configuration["MARKALLREAD_LIMIT"],0,-1);
		$Art = substr($Context->Configuration["MARKALLREAD_LIMIT"],-1,1);
		if ($Art == 'w') $Number = $Number * 7;		
		$limit = date("Y-m-d H:i:s",mktime() - ($Number * 86400));
        $sql = $Context->ObjectFactory->NewContextObject($Context, 'SqlBuilder');
        $sql->SetMainTable('Discussion','d');
        $sql->AddSelect(array('DiscussionID'), 'd');
		$sql->AddWhere('d', 'DateLastActive', '',$limit, '>=');
		$result = $Context->Database->Select($sql, 'OldDiscussions', 'OldDiscussionsTable', 'An error occurred while listing the Discussion Data.');
		while($rows = $Context->Database->GetRow($result)) {
			$value = CheckRead($Context,$rows['DiscussionID'],$UserID);
			if ($value < 2) MarkRead($Context,$rows['DiscussionID'],$value);
		}
	}

	function MarkRead(&$Context,$DiscussionID,$value) {
		$UserID = $Context->Session->UserID;
        $sql = $Context->ObjectFactory->NewContextObject($Context, 'SqlBuilder');
        $sql->SetMainTable('Comment','c');
        $sql->AddSelect(array('CommentID'), 'c');
		$sql->AddWhere('c', 'DiscussionID', '',$DiscussionID, '=');
        $sql->AddWhere('c', 'Deleted', '',0, '=');
		$result = $Context->Database->Select($sql, 'OldDiscussions', 'OldDiscussionsTable', 'An error occurred while listing the Discussion Data.');
		$rows = $Context->Database->RowCount($result);
		if ($value == 0){
         $sql = $Context->ObjectFactory->NewContextObject($Context, 'SqlBuilder');
         $sql->SetMainTable('UserDiscussionWatch', 'w');
         $sql->AddFieldNameValue('UserID', $UserID);
         $sql->AddFieldNameValue('DiscussionID', $DiscussionID);
         $sql->AddFieldNameValue('CountComments', $rows);
         $sql->AddFieldNameValue('LastViewed', MysqlDateTime());		 
         $Context->Database->Insert($sql,'Insert','MakeRead','An error occurred while logging your IP address.'); 
		}
		if ($value == 1){
			$sql = $Context->ObjectFactory->NewContextObject($Context, 'SqlBuilder');
			$sql->SetMainTable('UserDiscussionWatch', 'w');
      		$sql->AddFieldNameValue('LastViewed', MysqlDateTime());
			$sql->AddFieldNameValue('CountComments', $rows);
            $sql->AddWhere('w', 'DiscussionID', '', $DiscussionID, '=', 'and');
            $sql->AddWhere('w', 'UserID', '', $UserID, '=');
			$Context->Database->Update($sql,'Update','MakeRead','An error occurred while updating your profile.'); 
		} 
	}	
	
	function MarkCategoryRead(&$Context,$CategoryID) {
		$UserID = $Context->Session->UserID;
		$Number = substr($Context->Configuration["MARKALLREAD_LIMIT"],0,-1);
		$Art = substr($Context->Configuration["MARKALLREAD_LIMIT"],-1,1);
		if ($Art == 'w') $Number = $Number * 7;		
		$limit = date("Y-m-d H:i:s",mktime() - ($Number * 86400));
        $sql = $Context->ObjectFactory->NewContextObject($Context, 'SqlBuilder');
        $sql->SetMainTable('Discussion','d');
        $sql->AddSelect(array('DiscussionID'), 'd');
		$sql->AddWhere('d', 'CategoryID', '',$CategoryID, '=');
		$sql->AddWhere('d', 'DateLastActive', '',$limit, '>=');
		$result = $Context->Database->Select($sql, 'OldDiscussions', 'OldDiscussionsTable', 'An error occurred while listing the Discussion Data.');
		while($rows = $Context->Database->GetRow($result)) {
			$value = CheckRead($Context,$rows['DiscussionID'],$UserID);
			if ($value < 2) MarkRead($Context,$rows['DiscussionID'],$value);
		}
	}	

	function CheckRead(&$Context,$DiscussionID) {
		$UserID = $Context->Session->UserID;
        $sql = $Context->ObjectFactory->NewContextObject($Context, 'SqlBuilder');
        $sql->SetMainTable('Comment','c');
        $sql->AddSelect(array('CommentID'), 'c');
		$sql->AddWhere('c', 'DiscussionID', '',$DiscussionID, '=');
		$sql->AddWhere('c', 'Deleted', '',0, '=');
		$result = $Context->Database->Select($sql, 'OldDiscussions', 'OldDiscussionsTable', 'An error occurred while listing the Discussion Data.');
		$rows = $Context->Database->RowCount($result);
        $sql = $Context->ObjectFactory->NewContextObject($Context, 'SqlBuilder');
        $sql->SetMainTable('UserDiscussionWatch','c');
        $sql->AddSelect(array('CountComments'), 'c');
		$sql->AddWhere('c', 'DiscussionID', '',$DiscussionID, '=');
		$sql->AddWhere('c', 'UserID', '',$UserID, '=');
		$result = $Context->Database->Select($sql, 'OldDiscussions', 'OldDiscussionsTable', 'An error occurred while listing the Discussion Data.');
		$rows2 = $Context->Database->GetRow($result);		
		if ($rows2['CountComments'] == 0) return 0;
		if ($rows > $rows2['CountComments'] AND $rows2['CountComments'] > 0) return 1;
		if ($rows == $rows2['CountComments']) return 2;
	}

	
	if (in_array($Context->SelfUrl, array('index.php','categories.php'))) {
		$CategoryID = ForceIncomingString('CategoryID',0);
		if ($CategoryID  > 0) {
			$Panel->AddListItem($Context->GetDefinition('Options'),$Context->GetDefinition('MarkCategoryRead'),"?PostBackAction=MarkCategoryRead&amp;CategoryID=".$CategoryID,"","id=\"MarkCategoryRead\"");
		}
		$Panel->AddListItem($Context->GetDefinition('Options'),$Context->GetDefinition('MarkAllRead'),"?PostBackAction=MarkAllRead","","id=\"MarkAllRead\"");
	}

	$PostBackAction = ForceIncomingString("PostBackAction", "");
	if ($PostBackAction == 'MarkAllRead') {
		MarkAllRead($Context);
	}
	if ($PostBackAction == 'MarkCategoryRead') {
		$CategoryID = ForceIncomingInt("CategoryID", "0");
		if ($CategoryID > 0) MarkCategoryRead($Context,$CategoryID);
	}
}
?>
